package com.cjack.frenchexcel.controller;

import com.cjack.frenchexcel.config.AjaxResult;
import com.cjack.frenchexcel.config.CommonConfig;
import com.cjack.frenchexcel.config.LayPage;
import com.cjack.frenchexcel.model.GoodsModel;
import com.cjack.frenchexcel.service.GoodsService;
import com.cjack.frenchexcel.service.UserAdminService;
import com.cjack.frenchexcel.table.GoodsTable;
import com.cjack.frenchexcel.util.ModelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsEndpoint extends BaseEndpoint{

    @Autowired
    GoodsService service;
    @Autowired
    UserAdminService userAdminService;

    /**
     * 列表
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult list( HttpSession session, LayPage page, GoodsModel model) {
        try{
            Object adminUserId = session.getAttribute( CommonConfig.SESSION_NAME);
            model.setAdminUserId( (Long)adminUserId);
            Page< GoodsTable> resultPage = service.list( page, model);
            List<GoodsModel> goods = ModelUtils.copyListModel( resultPage.getContent(), GoodsModel.class);
            for( GoodsModel good : goods){
                //商品所属员工工号
                good.setJobNumber( userAdminService.get( good.getAdminUserId()).getJobNumber());
            }
            page.setData( goods);
            page.setCount( Long.valueOf( resultPage.getTotalElements()).intValue());
            return AjaxResult.SUCCESS( page);
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 列表
     * @return
     */
    @RequestMapping(value = "/listNoPage", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult listNoPage( HttpSession session, GoodsModel model) {
        try{
            Object adminUserId = session.getAttribute( CommonConfig.SESSION_NAME);
            model.setAdminUserId( (Long)adminUserId);
            List< GoodsTable> tables = service.list( model);
            List<GoodsModel> goods = ModelUtils.copyListModel( tables, GoodsModel.class);
            for( GoodsModel good : goods){
                //商品所属员工工号
                try {
                    good.setJobNumber( userAdminService.get( good.getAdminUserId()).getJobNumber());
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            return AjaxResult.SUCCESS( goods);
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult addOrUpdate( GoodsModel model) {
        try{
            if( isAdd( model.getId())){
                model.setCreateTime( new Date());
            }
            service.add( model);
            return AjaxResult.SUCCESS();
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult del( @RequestParam("ids") String ids) {
        try{
            service.del( ids);
            return AjaxResult.SUCCESS( "删除成功");
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
